package cn.xianshu.controller;


import cn.xianshu.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 浏览器调用  订单服务方法，调用用户服务方法
     * @param id
     * @return
     */
    @HystrixCommand(fallbackMethod = "fallbackMethod")
    @RequestMapping("/getById/{id}")
    public User getById(@PathVariable("id") Long id){
        // RestTemplate远程调用用户服务
        // String url = "http://localhost:1030/getById/" + id;
        String url = "http://USER-SERVER/getById/" + id;
        User user = restTemplate.getForObject(url, User.class);
        System.out.println("拿到用户服务返回的User" + user.getUsername());
        return user;
    }

    // 降级方法
    public User fallbackMethod(@PathVariable("id") Long id){
        // 兜底数据
        return new User(-1L, "", "系统异常，用户获取失败!");
    }

}
